<template>
	<view></view>
</template>

<script>
let rewardedVideoAdFree = null;
// 抖音配置
let ttConfig = require("../../../util/ttConfigSet.js");
export default {
	props: {
		isIaaAd: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			ttConfigMess: {},
			systemInfo: {}
		};
	},
	onReady() {
		let ttAccountInfo = tt.getEnvInfoSync() && tt.getEnvInfoSync().microapp
		this.ttConfigMess = ttConfig[ttAccountInfo.appId] || {}
		this.handleAdView()
		this.systemInfo = tt.getSystemInfoSync()
	},

	methods: {
		isShowAd() {
			console.log('激励视频触发')
			if (this.ttConfigMess && this.ttConfigMess.adLockIdFree) {
				this.$statisAd({
					action: 'REQUEST',
					adId: this.ttConfigMess.adLockIdFree,
					adSlot: 2,
					// episodicDramaId: String(nowItem.id),
					// videoId: String(nowItem.videoId),
					// authorId: this.compInfo.laikanAccountId ? String(this.compInfo.laikanAccountId) : '0',
					// fakeId: String(nowItem.compilationsFakeId),
				});
				rewardedVideoAdFree.show().then(() => {
					this.$statisAd({
						action: 'EXPOSURE',
						adId: this.ttConfigMess.adLockIdFree,
						adSlot: 2,
						// episodicDramaId: String(nowItem.id),
						// videoId: String(nowItem.videoId),
						// authorId: this.compInfo.laikanAccountId ? String(this.compInfo.laikanAccountId) : '0',
						// fakeId: String(nowItem.compilationsFakeId),
					});
				}).catch(() => {
					rewardedVideoAdFree
						.load()
						.then(() => {
							rewardedVideoAdFree.show()
							this.$statisAd({
								action: 'EXPOSURE',
								adId: this.ttConfigMess.adLockIdFree,
								adSlot: 2,
								// episodicDramaId: String(nowItem.id),
								// videoId: String(nowItem.videoId),
								// authorId: this.compInfo.laikanAccountId ? String(this.compInfo.laikanAccountId) : '0',
								// fakeId: String(nowItem.compilationsFakeId),
							});
						})
						.catch(err => {
							uni.hideLoading()
							let title = this.systemInfo.appName === 'douyin_lite' ? '抖音极速版暂不支持此功能' : '广告溜走了,请稍后再试试'
							uni.showToast({
								title: title,
								icon: 'none'
							})
						})
				})
			} else {
				uni.hideLoading()
				let title = this.systemInfo.appName === 'douyin_lite' ? '抖音极速版暂不支持此功能' : '广告溜走了,请稍后再试试'
				uni.showToast({
					title: title,
					icon: 'none'
				})
			}
		},

		handleAdView() {
			// 获取配置信息
			if (this.ttConfigMess && this.ttConfigMess.adLockIdFree) {
				// 创建实例
				rewardedVideoAdFree = tt.createRewardedVideoAd({
					adUnitId: this.ttConfigMess.adLockIdFree || "5zsfl0p43klyg5xbiu",
				})

				rewardedVideoAdFree.onError((err) => {
					console.log(err, 'err');
					uni.hideLoading()

					// let title = this.systemInfo.appName === 'douyin_lite' ? '抖音极速版暂不支持此功能' : '广告溜走了,请稍后再试试'
					// uni.showToast({
					// 	title: title,
					// 	icon: 'none'
					// })
				})

				rewardedVideoAdFree.onClose(({ isEnded }) => {
					uni.hideLoading()
					console.log('关闭', isEnded)
					if (isEnded && !this.isIaaAd) { // 观看完毕解锁
						this.$emit('closeAdBoxAndUnlock', true)
					} else { // 未观看完毕
						// this.$emit('closeAdBox', true)
					}
				})
				// 预加载资源
				rewardedVideoAdFree.load(() => {

				});
			}
		}
	},
	beforeDestroy() {
		rewardedVideoAdFree && rewardedVideoAdFree.offLoad()
		rewardedVideoAdFree && rewardedVideoAdFree.offClose()
		rewardedVideoAdFree = null
	}
}
</script>
